package cn.hicard.risk.action.bd;

import java.io.ByteArrayOutputStream;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import cn.hicard.risk.common.Constant;
import cn.hicard.core.Page;
import cn.hicard.core.PageResult;
import cn.hicard.core.Where;
import cn.hicard.risk.entity.BdBusiness;
import cn.hicard.risk.entity.BdMerchinfo;
import cn.hicard.risk.report.ReportTemplet;
import cn.hicard.risk.service.bd.BusinessService;
import cn.hicard.risk.service.bd.MerchInfoService;

public class MerchInfoFlex {

	/** 日志记录工具 */
	private static Logger logger = Logger.getLogger(Constant.LOGGER_MAIN);
	
	private MerchInfoService merchInfoService;
	
	private BusinessService businessService;

	public void setBusinessService(BusinessService businessService) {
		this.businessService = businessService;
	}

	public void setMerchInfoService(MerchInfoService merchInfoService) {
		this.merchInfoService = merchInfoService;
	}
	
	/**
	 * 根据相应条件搜索风险评估数据集合，结果以分页显示。<br/>
	 * 如果遇到单一字段的数值区间的搜索条件时，则将数值区间的开始位设在estimateStart中，数值区间的结束位设在estimateEnd中;<br/>
	 * 如果搜索条件为单个字段，则值设在estimateStart中。
	 * @param estimateStart
	 * @param estimateEnd
	 * @param page
	 * @return
	 * @throws Exception 
	 */
	public PageResult <Map<String, Object>> search(String txtHmId,String txtHmName,String txtHtId,String txtStatus,String txtAegisCompany,
			Page page
			) throws Exception{
		try {
			Where where =new Where(BdMerchinfo.class);
			where.setMode(Where.MODE_NOT_CHECK);
			where.addEqual("m.hm_id", txtHmId.trim());
			where.addLike("m.hm_name", txtHmName.trim());
			where.addEqual("b.ht_id",txtHtId.trim() );
			where.addEqual("m.status", txtStatus.trim());
			where.addEqual("b.aegis_company", txtAegisCompany.trim());
			return merchInfoService.getMerchList(where, page);
			
		} catch (Exception e) {
			logger.error("搜索时发生异常",e); 
			throw e;
		}
	}
	
	/**
	 * 根据汇卡商户号、汇卡终端号、银联商户号、银联终端号 查询业务信息
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public BdBusiness getBusinessDetailById(String hmId,String htId,String omId,String otId) throws Exception{
		BdBusiness business=new BdBusiness();
		try{
			business =businessService.getBusinessDetailByAllId(hmId, htId, omId, otId);
		}catch(Exception e){
			e.printStackTrace();
		}
		return business;
	}
	
	/**
	 * 黑名单报表
	 * @return
	 * @throws Exception 
	 * @throws Exception 
	 */
	public byte[] blackReport() throws Exception {
		
		ReportTemplet reportTemplet=new ReportTemplet();
		Where where =new Where(BdMerchinfo.class);
		where.setMode(Where.MODE_NOT_CHECK);
		where.addEqual("merch_degree", "5");
		List <Object[]>list=merchInfoService.getMerchReportList(where);
		String [] fileds=new String[]{"汇卡商户号","商户名称","最后修改时间","原因描述","风险级别"}; 

		//生成
		ByteArrayOutputStream bos =reportTemplet.createGeneralTemplet("黑名单报表","黑名单报表",fileds,list);

		return bos.toByteArray();
	}
}
